<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="开票编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入开票编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开票类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择开票类型" clearable>
          <el-option
            v-for="dict in invoice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="冲红状态" prop="changeStatus">
        <el-select v-model="queryParams.changeStatus" placeholder="请选择冲红状态" clearable>
          <el-option
            v-for="dict in red_invoice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提交时间" prop="searchDate">
        <el-date-picker clearable
          v-model="searchDate"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="confirmSearchDate">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['plat:invoice:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table class="fixed-scrollbar" v-horizontal-scroll border v-loading="loading" :data="list">
        <el-table-column label="开票编号" align="center" prop="number" width="130" show-overflow-tooltip />
        <el-table-column label="开票类型" align="center" prop="type" width="80">
          <template #default="scope">
            <dict-tag :options="invoice_type" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="冲红总金额（元）" align="center" prop="changeMoney" width="130">
          <template #default="scope">
            <span>{{ formatAmount(scope.row.changeMoney) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="冲红状态" align="center" prop="changeStatus" width="100">
          <template #default="scope">
            <dict-tag :options="red_invoice_status" :value="scope.row.changeStatus" />
          </template>
        </el-table-column>
        <el-table-column label="提交时间" align="center" prop="changeTime" width="160">
          <template #default="scope">
            <span>{{ parseTime(scope.row.changeTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="抬头名称" align="center" prop="invoiceHeader" min-width="150" show-overflow-tooltip />
        <el-table-column label="税号" align="center" prop="taxNumber" width="180" show-overflow-tooltip />
        <el-table-column label="单位地址" align="center" prop="address" min-width="180" show-overflow-tooltip />
        <el-table-column label="电话" align="center" prop="telephone" width="120" />
        <el-table-column label="开户银行" align="center" prop="bank" min-width="150" show-overflow-tooltip />
        <el-table-column label="银行账号" align="center" prop="account" width="160" show-overflow-tooltip />
        <el-table-column label="电子邮箱" align="center" prop="email" width="150" show-overflow-tooltip />
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="85">
          <template #default="scope">
            <template v-if="scope.row.changeStatus == 2">
              <el-tooltip content="上传冲红发票" placement="top">
                <el-button link type="primary" icon="Upload" @click="uploadInvoiceFile(scope.row)" v-hasPermi="['plat:invoice:edit']"></el-button>
              </el-tooltip>
            </template>
            <template v-if="scope.row.changeStatus == 3">
              <el-tooltip content="查看冲红发票" placement="top">
                <el-button link type="primary" icon="View" @click="previewInvoice(scope.row)" v-hasPermi="['plat:invoice:query']"></el-button>
              </el-tooltip>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <input class="file-input" ref="fileInput" type="file" accept="image/*" @change="invoiceFileChange" />

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Redinvoice">
import { listInvoice, uploadInvoice } from "@/api/order/invoice"

const { proxy } = getCurrentInstance()
const { invoice_type, red_invoice_status } = proxy.useDict('invoice_type', 'red_invoice_status')

const fileInput = ref()
const list = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const searchDate = ref([])
const invoiceId = ref('')

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    listType: 2,
    number: null,
    changeStatus: null,
    type: null,
    startTime: null,
    endTime: null,
  }
})

const { queryParams } = toRefs(data)

const confirmSearchDate = (e) => {
  if (e && e.length) {
    queryParams.value.startTime = e[0]
    queryParams.value.endTime = e[1]
  } else {
    queryParams.value.startTime = null
    queryParams.value.endTime = null
  }
  handleQuery()
}

/** 查询发票列表 */
function getList() {
  loading.value = true
  listInvoice(queryParams.value).then(response => {
    list.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 预览发票 */
function previewInvoice(row) {
  window.open(row.changePic, '__blank')
}

/** 上传发票 */
function uploadInvoiceFile(row) {
  invoiceId.value = row.id
  fileInput.value.click()
}

function invoiceFileChange(e) {
  const files = e.target.files
  if (files && files.length) {
    const file = files[0]
    if (file.size > 10 * 1024 * 1024) {
      return proxy.$modal.msgWarning('上传图片大小不得超过10M')
    }
    const reader = new FileReader()
    reader.readAsDataURL(file)
    reader.onload = () => {
      const params = {
        id: invoiceId.value,
        changePic: reader.result
      }
      uploadInvoice(params).then(res => {
        proxy.$modal.msgSuccess('上传成功')
        getList()
      })
    }
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  searchDate.value = []
  queryParams.value.startTime = null
  queryParams.value.endTime = null
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('plat/invoice/export', {
    ...queryParams.value
  }, `冲红管理_${proxy.getFileImportName()}.xlsx`)
}

getList()
</script>

<style lang="scss" scoped>
.file-input {
  position: fixed;
  top: 200px;
  left: 500px;
  width: 3px;
  height: 3px;
  opacity: 0;
}
</style>
